package com.wondersgroup.insurance.dnuilearn.views.xformode;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.wondersgroup.insurance.dnuilearn.R;

/**
 * Created by Administrator on 2017/5/15.
 */

public class GuaGuaCard  extends View {
    Paint mPaint;

    Bitmap skinBmp, textBmp,tmpBmp;

    Path path = new Path();


    public GuaGuaCard(Context context) {
        this(context, null);
    }

    public GuaGuaCard(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GuaGuaCard(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(View.LAYER_TYPE_SOFTWARE,null);

        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(40);
        textBmp = BitmapFactory.decodeResource(getResources(), R.drawable.guaguaka_text1);
        skinBmp = BitmapFactory.decodeResource(getResources(), R.drawable.guaguaka);
        tmpBmp = Bitmap.createBitmap(skinBmp.getWidth(),skinBmp.getHeight(), Bitmap.Config.ARGB_8888);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(textBmp,0,0,mPaint);


//        Canvas c = new Canvas(tmpBmp);
//        c.drawBitmap(skinBmp,0,0,mPaint);
//        if (!path.isEmpty()){
//            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
//            c.drawPath(path,mPaint);
//            mPaint.setXfermode(null);
//        }
//
//        canvas.drawBitmap(tmpBmp,0,0,mPaint);

        int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
        //先把手指轨迹画到目标Bitmap上
        Canvas c = new Canvas(tmpBmp);
        c.drawPath(path,mPaint);

        //然后把目标图像画到画布上
        canvas.drawBitmap(tmpBmp,0,0,mPaint);

        //计算源图像区域
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
        canvas.drawBitmap(skinBmp,0,0,mPaint);

        mPaint.setXfermode(null);
        canvas.restoreToCount(layerId);

    }

    float x ,y;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                path.moveTo(event.getX(),event.getY());
                x = event.getX();
                y = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float endX = (x+event.getX())/2;
                float endY = (y+event.getY())/2;

                path.quadTo(x,y,endX,endY);
                x = endX;
                y = endY;
                invalidate();
                break;
        }
        return true;
    }
}
